package com.web.servlet;

import com.alibaba.fastjson2.JSON;
import com.web.JDBCUtils;
import com.web.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/abc")
public class FindStudentsServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
            }

    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //设置内容类型
        resp.setContentType("application/json;charset=utf-8");
        //查询学生信息
        List<Student> students= JDBCUtils.query(Student.class,"select * from student");
        //转换数据为JSON格式
        String str = JSON.toJSONString(students);
        resp.getWriter().write(str);
    }
}
